class_name ExportModDataUtil


static func export_mod_data():
	DataSource.init_data()
	
	var config = export_config()
	var dic = export_dic()
	var shop = export_shop()
	var npc = export_npc()
	var map = export_map()
	var server = export_server()
	var dialog = export_dialog()
	var drama = export_drama()
	var sprite = export_sprite()
	
	var skill = export_skill()
	
	var data = {
		"sprite": sprite,
		"dic": dic,
		"map": map,
		"item": shop,
		"npc": npc,
		"drama": drama,
		"dialog": dialog,
		"config": config,
		"server": server,
		"skill": skill,
		"areaInfo": ModeConfig.config.duplicate(true)
	}
	
	
	
	return data
	
	
static func _run():
	var mode_data = export_mod_data()
	
	FileHelper.save("area_mod_data.json", mode_data)
	
	print("导出成功!")


static func export_server():
	var field_appoint_sprite = []
	for item in DataSource.get_data_list(DataSource.appoint_sprite):
		field_appoint_sprite.append(item)
#	var field_appoint_sprite = ModeConfig.field_appoint_sprite
	
	var correlations = []
	for map_register in DataSource.get_data_list(DataSource.map_register):
		if map_register.get("correlation", 0) > 0:
			correlations.append({
				"id": correlations.size()+1,
				"mapId": map_register["id"],
				"correlationMapId": map_register.get("correlation", 0)
			})
	
	
	var fight_sprite = []
	
	for fight in DataSource.get_data_list(DataSource.npc_fight):
		var sprite_list = fight.get("spriteList", []) 
		
		for sprite in sprite_list:
			var data = {
				"id": fight_sprite.size()+1,
				"npcFightId": fight["id"],
			}
			
			for key in sprite.keys():
				data[key] = sprite[key]
			
			fight_sprite.append(data)
	
	
	var pick_item = DataSource.get_data_list(DataSource.pick_item)
	var pick_sprite = DataSource.get_data_list(DataSource.pick_sprite)
	
	fill_area_info(field_appoint_sprite)
	fill_area_info(correlations)
	fill_area_info(fight_sprite)
	fill_area_info(pick_item)
	fill_area_info(pick_sprite)
	
	return {
		"poke_game_data_field_appoint_pokemon": field_appoint_sprite,
		"poke_game_data_npc_fight_map_correlation": correlations,
		"poke_game_data_npc_fight_pokemon": fight_sprite,
		"poke_game_data_pick_dialog_item": pick_item,
		"poke_game_data_pick_dialog_pokemon": pick_sprite,
	}
	


# 导出字典
static func export_dic():
	var gyms =  DataSource.get_data_list(DataSource.gyms)
	for item in gyms:
		if item.get("icon"):
			item["icon"] = replace_assets_path(item["icon"])
#	var consume_coin = ModeConfig.consume_coin
	var consume_coin = []
	for item in DataSource.get_data_list(DataSource.coin_consume):
		consume_coin.append(item)
	
	fill_area_info(consume_coin)
	fill_area_info(gyms)
	
	return {
		"gyms": gyms,
		"consumeCoin": consume_coin
	}
	

# 导出商店数据
static func export_shop():
	var shops = DataSource.get_data_list(DataSource.shop)
	var shop_details = DataSource.get_data_list(DataSource.shop_details)
	
	fill_area_info(shops)
	fill_area_info(shop_details)
	
	return {
		"shops": shops,
		"shopDetails": shop_details,
	}
	

# 导出配置
static func export_config():
	var conditions = DataSource.get_data_list(DataSource.conditions)
	
	fill_area_info(conditions)
		
	return {
		"condition": conditions
	}


static func export_dialog():
	var dialigs = []
	

	for dialog in DataSource.get_data_list(DataSource.dialig):
		dialigs.append(engine_dilag_to_server(dialog))

	fill_area_info(dialigs)
	
	return {
		"dialogs": dialigs
	}


static func engine_dilag_to_server(dialog):
	
	var new_dialog = {
		"id": dialog["id"],
		"name": dialog["name"],
		"remark": dialog["remark"],
		"data":{
			"type": dialog["type"]
		}
	}
	
	if dialog["type"] == 0:
		var list = []
		
		var contents = []
		var paragraph = {}
		var select_paragraph = {}
		
		var index = 1
		for content in dialog["contents"]:
			contents.append(content["content"])
			
			if content["paragraph"]:
				paragraph[index] = content["paragraph"]
				
			if content["select_paragraph"]:
				var data = {
					"select":[]
				}
				
				select_paragraph[index] = data
				
				var select_index = 1
				for i in content["select_paragraph"]:
					data["select"].append(i["title"])
					
					if i["paragraph"]:
						data[select_index] = i["paragraph"]
						
					select_index += 1
			index += 1
			
		new_dialog["data"]["contents"] = contents
		new_dialog["data"]["paragraph"] = paragraph
		new_dialog["data"]["select_paragraph"] = select_paragraph
	else:
		new_dialog["data"]["dialogs"] = dialog["contents"] 
	
	return new_dialog


		
static func export_drama():
	
	var core_list = []
	var main_list = DataSource.get_data_list(DataSource.drama)
	
	for drama in main_list:
		var core = {}
		core["id"] = drama["id"]
		core["name"] = drama["name"]
		core["remark"] = drama["remark"]
		
		var core_data = {
			"type": drama["type"],
			"drama_start_state": drama.get("drama_start_state", []),
			"drama_sequence": drama.get("drama_sequence", []),
			"drama_hinder": drama.get("drama_hinder", []),
			"drama_next_normal_hinder": drama.get("drama_next_normal_hinder", []),
		}
		

		core["data"] = core_data
		
		core_list.append(core)
			
	
	fill_area_info(core_list)
	fill_area_info(main_list)

	return {
		"core": core_list,
		"main": main_list
	}
	
static func export_npc():
	var npc_fights = DataSource.get_data_list(DataSource.npc_fight)
	
	fill_area_info(npc_fights)
	for item in npc_fights:
		item["verticalPaint"] = replace_assets_path(item["verticalPaint"])
		pass
	return {
		"fight": npc_fights
	}


static func replace_assets_path(path):
	if path:
		return path.replace("res://map/", "")
	else:
		return ""
		

static func export_map():
	var map_area = DataSource.get_data_list(DataSource.map_area, true)
	for i in map_area:
		i["musicName"] = replace_assets_path(i["musicName"])
		
	var map_door = DataSource.get_data_list(DataSource.map_door, true)
	for i in map_door:
		i["res"] = replace_assets_path(i["res"])
		
	var map_npc = DataSource.get_data_list(DataSource.map_npc, true)
	
	for i in map_npc:
		i["npcRes"] = replace_assets_path(i["npcRes"])
	
	var map_prop = DataSource.get_data_list(DataSource.map_prop, true)
	
	for i in map_prop:
		i["res"] = replace_assets_path(i["res"])
		
	var map_register = DataSource.get_data_list(DataSource.map_register)
	var map_tree = DataSource.get_data_list(DataSource.map_trees)
	var map_center_recover = DataSource.get_data_list(DataSource.map_center_recover)
	var map_decorations = DataSource.get_data_list(DataSource.map_decoration, true)
	
	for i in map_decorations:
		i["res"] = replace_assets_path(i["res"])
		
	var map_interactive = DataSource.get_data_list(DataSource.map_interactive, true)
	
	for i in map_interactive:
		i["res"] = replace_assets_path(i["res"])
		i["audio"] = replace_assets_path(i["audio"])
	
	
	fill_area_info(map_area)
	fill_area_info(map_door)
	fill_area_info(map_npc)
	fill_area_info(map_prop)
	fill_area_info(map_register)
	fill_area_info(map_tree)
	fill_area_info(map_center_recover)
	fill_area_info(map_decorations)
	fill_area_info(map_interactive)
	
	return {
		"map": map_area,
		"door": map_door,
		"npc": map_npc,
		"prop": map_prop,
		"register": map_register,
		"tree": map_tree,
		"pokemonCenterRecovers":map_center_recover,
		"decorations":map_decorations,
		"interactive":map_interactive,
	}


static func export_sprite():
	var sprite_area = DataSource.get_data_list(DataSource.map_sprite_area)
	var sprite_area_item = DataSource.get_data_list(DataSource.map_sprite_area_item)
	
	var init = [
		# 不在直接读取
#		{"id": 1,"pokeId": ModeConfig.init_sprite[0]},
#		{"id": 2,"pokeId": ModeConfig.init_sprite[1]},
#		{"id": 3,"pokeId": ModeConfig.init_sprite[2]},
	]
	
	for item in DataSource.get_data_list(DataSource.init_sprite):
		init.push_back(item)
		pass
	var attach_items = []
	
	
	for item in DataSource.get_data_list(DataSource.poke_item):
		var item_list = item.get("itemList", [])
		
		for attach_item in item_list:
			var data = {
				"id": attach_items.size()+1,
				"pokemonId": item["id"]
			}
			
			for key in attach_item.keys():
				data[key] = attach_item[key]
				
			attach_items.append(data)
	
		
#	var pick_pokemon_change =  ModeConfig.pick_pokemon_change
	var pick_pokemon_change =  []
	for item in DataSource.get_data_list(DataSource.sprite_change):
		pick_pokemon_change.append(item)
		pass
	
	fill_area_info(sprite_area)
	fill_area_info(sprite_area_item)
	fill_area_info(init)
	fill_area_info(attach_items)
	fill_area_info(pick_pokemon_change)
	
	return {
		"fieldPokemonAreas": sprite_area,
		"fieldPokemonItems": sprite_area_item,
		"init": init,
		"attachItems": attach_items,
		"pickPokemonChanges": pick_pokemon_change
	}


static func export_skill():
	var teach_shop = DataSource.get_data_list(DataSource.teach_shop)
	fill_area_info(teach_shop)
	
	return {
		"teachShop": teach_shop,
	}


# 填充区域信息
static func fill_area_info(list):
	var area_id = ModeConfig.config["areaId"]
	for item in list:
		item["areaId"] = area_id
		
		if !item.has("id"):
			print(item)
		
		item["uniqueIndex"] = item["id"]
		
		# 删除id字段
#		item.erase("id")

	
